package com.xiyu.service.service.files;

import com.xiyu.service.vo.files.realEstate.RealEstateSingleGetOutput;
import com.xiyu.service.vo.files.realEstate.RealEstateUpdateInput;
import com.xiyu.service.vo.files.realEstate.RealEstateCreateInput;
import com.xiyu.service.model.files.certificate.FilesRealEstate;
import com.xiyu.service.repository.files.certificate.FilesRealEstateRepository;
import com.xiyu.service.vo.files.realEstate.RealEstatePageQueryOutput;
import com.xiyu.service.vo.files.realEstate.RealEstatePageQueryInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.util.entity.EntityUtils;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.files.RealEstateConvert;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.files.RealEstateErrorCode.*;

/**
 * 不动产权证 Service 实现类
 */
@Service
@Validated
public class RealEstateServiceImpl implements RealEstateService {

    @Resource
    private FilesRealEstateRepository filesRealEstateRepository;




    @Override
    public PageResult<RealEstatePageQueryOutput> pageQuery(RealEstatePageQueryInput inputVO) {
        Page<FilesRealEstate> pageFilesRealEstate = filesRealEstateRepository.pageQuery(inputVO);
        List<RealEstatePageQueryOutput> listFilesRealEstate = RealEstateConvert.INSTANCE.pageQueryOutputConvert(pageFilesRealEstate.getRows());
        return new PageResult<>(listFilesRealEstate, pageFilesRealEstate.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(RealEstateCreateInput inputVO) {
        FilesRealEstate newFilesRealEstate = RealEstateConvert.INSTANCE.createInputConvert(inputVO);
        newFilesRealEstate = filesRealEstateRepository.insert(newFilesRealEstate);
        return newFilesRealEstate.id();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(RealEstateUpdateInput inputVO) {
        Optional<FilesRealEstate> optionalOldFilesRealEstate = filesRealEstateRepository.findById(inputVO.getId());
        if(!optionalOldFilesRealEstate.isPresent()){
            throw exception(FILES_REAL_ESTATE_NOT_EXIST);
        }
        FilesRealEstate updateFilesRealEstate = RealEstateConvert.INSTANCE.updateInputConvert(inputVO);
        if (!EntityUtils.isEquals(optionalOldFilesRealEstate.get(), updateFilesRealEstate))
            filesRealEstateRepository.update(updateFilesRealEstate);
        return true;
    }

    @Override
    public RealEstateSingleGetOutput singleGet(Long id) {
        Optional<FilesRealEstate> optionFilesRealEstate = filesRealEstateRepository.findById(id);
        if(!optionFilesRealEstate.isPresent()){
            throw exception(FILES_REAL_ESTATE_NOT_EXIST);
        }
        return RealEstateConvert.INSTANCE.singleGetOutputConvert(optionFilesRealEstate.get());

    }

}
